Skip to content

Conversation

@bugadani
Copy link
Contributor

@bugadani bugadani commented Dec 1, 2025

This PR makes three changes to the UART driver:

  • It corrects unclear/misleading documentation
  • It stabilizes read_ready and expands on its documentation, to make it possible to truly read without blocking. Will be proposed separately.
  • It changes wait_for_buffered_data:
    • there is no more need to loop, we clear FifoFull while draining the buffer
    • simplify the code by setting the fifo threshold unconditionally
  • the PR also drains the fifo first in read_exact_async

@jimy-byerley could you please see if this PR addresses your issue? It's not easy to reproduce on the MCU itself, and I'm not sure if we can make a reliable test case for it.

@bugadani
Copy link
Contributor Author

bugadani commented Dec 1, 2025

/hil full

@github-actions
Copy link

github-actions bot commented Dec 1, 2025

Triggered full HIL run for #4586.

Run: https://github.com/esp-rs/esp-hal/actions/runs/19830655480

Status update: ❌ HIL (full) run failed (conclusion: failure).

@bugadani bugadani force-pushed the uart-changes branch 4 times, most recently from ef47d58 to 6c02337 Compare December 2, 2025 15:35
@bugadani bugadani changed the title Uart changes Fix some UART-related issues Dec 2, 2025
@bugadani
Copy link
Contributor Author

bugadani commented Dec 2, 2025

/hil full

@github-actions
Copy link

github-actions bot commented Dec 2, 2025

Triggered full HIL run for #4586.

Run: https://github.com/esp-rs/esp-hal/actions/runs/19865042209

Status update: ❌ HIL (full) run failed (conclusion: failure).

@jimy-byerley
Copy link

I just tested the current commit of your branch 1f468654d4019192b4de6971e3eb735fb7368443, it doesn't fix the issue 😢 still FifoOverflowed

@bugadani
Copy link
Contributor Author

bugadani commented Dec 2, 2025

Thank you, I'll spend time to try and better reproduce this issue. In the mean time, can you try changing the fifo_full_threshold config option to a lower value and see if it changes anything?

@bugadani
Copy link
Contributor Author

bugadani commented Dec 3, 2025

I think there are enough changes here to move forward. While this doesn't fix #4523, the PR fixes some minor perf issues and doesn't seem to make things worse with them.

@bugadani
Copy link
Contributor Author

bugadani commented Dec 3, 2025

/hil full

@github-actions
Copy link

github-actions bot commented Dec 3, 2025

Triggered full HIL run for #4586.

Run: https://github.com/esp-rs/esp-hal/actions/runs/19888034284

Status update: HIL (full) run is still in progress or status unknown._

@bugadani bugadani marked this pull request as ready for review December 3, 2025 08:58
Copilot AI review requested due to automatic review settings December 3, 2025 08:58
Copilot finished reviewing on behalf of bugadani December 3, 2025 09:03
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes several UART-related issues including unclear documentation and potential race conditions in async read operations. The changes primarily focus on improving the reliability of read_exact_async and clarifying blocking behavior in documentation.

  • Corrects misleading documentation that stated read methods would not block
  • Refactors wait_for_buffered_data to fix issue #4523 by changing from a while loop to conditional waiting
  • Adds buffer draining in read_exact_async to prevent unexpected FifoOverflowed errors

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
esp-hal/src/uart/mod.rs Refactors async UART receive logic to prevent race conditions, updates blocking behavior documentation for read/write_ready/read_ready methods, and adds defensive event clearing in read_buffered
esp-hal/CHANGELOG.md Documents the documentation corrections and bug fix for read_exact_async
Comments suppressed due to low confidence (1)

esp-hal/src/uart/mod.rs:1192

  • [nitpick] Inconsistent method call pattern detected. Lines 1182 and 1192 call self.uart.info().read_buffered(buf) directly, while line 1159 (in read_async) calls self.read_buffered(buf).

For consistency and better encapsulation, consider using self.read_buffered(buf) instead, which internally calls self.uart.info().read_buffered(buf) as seen in line 1351.

        let read = self.uart.info().read_buffered(buf)?;
        buf = &mut buf[read..];

        while !buf.is_empty() {
            // No point in listening for timeouts, as we're waiting for an exact amount of
            // data. On ESP32 and S2, the timeout interrupt can't be cleared unless the FIFO
            // is empty, so listening could cause an infinite loop here.
            self.wait_for_buffered_data(buf.len(), buf.len(), false)
                .await?;

            let read = self.uart.info().read_buffered(buf)?;

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Member

@MabezDev MabezDev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks!

Should we hold off on the backport PR until the OG issue is resolved?

@bugadani
Copy link
Contributor Author

bugadani commented Dec 3, 2025

I think this is valuable enough on its own.

@MabezDev MabezDev added this pull request to the merge queue Dec 3, 2025
@MabezDev MabezDev added the esp-hal-backport Backport this PR to the latest esp-hal-x.y.x branch.” label Dec 3, 2025
Merged via the queue into esp-rs:main with commit d3d2a77 Dec 3, 2025
40 checks passed
@github-actions
Copy link

github-actions bot commented Dec 3, 2025

Backport failed for esp-hal-1.0.x, because it was unable to cherry-pick the commit(s).

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin esp-hal-1.0.x
git worktree add -d .worktree/backport-4586-to-esp-hal-1.0.x origin/esp-hal-1.0.x
cd .worktree/backport-4586-to-esp-hal-1.0.x
git switch --create backport-4586-to-esp-hal-1.0.x
git cherry-pick -x d3d2a77759e5c5e0623055f093c5f4b2515414f4

@bugadani bugadani deleted the uart-changes branch December 3, 2025 13:40
@bugadani bugadani restored the uart-changes branch December 3, 2025 13:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

esp-hal-backport Backport this PR to the latest esp-hal-x.y.x branch.”

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants